﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="DeploymentOperationBase.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.DeploymentManager.DeploymentWorkflow
{
    using System;

    public abstract class DeploymentOperationBase : IDeploymentOperation
    {
        protected DeploymentOperationBase()
        {
        }

        protected DeploymentOperationBase(int progressWeight)
        {
            if (progressWeight <= 0)
            {
                throw new ArgumentOutOfRangeException("progressWeight", "Progress weight must not be less than or equal to 0.");
            }

            this.ProgressWeight = progressWeight;
        }

        public virtual OperationResult Execute(DeploymentContext context)
        {
            var result = this.ExecuteOverride(context);
            this.RaiseOperationCompletedEvent(result);

            return result;
        }

        public event EventHandler<ProgressChangedEventArgs> ProgressChanged;

        public event EventHandler<OperationCompletedEventArgs> Completed;

        public virtual int ProgressWeight { get; protected set; }

        protected abstract OperationResult ExecuteOverride(DeploymentContext context);

        protected void RaiseOperationCompletedEvent(OperationResult result)
        {
            if (this.Completed != null)
            {
                this.Completed(this, new OperationCompletedEventArgs(result));
            }
        }

        protected void RaiseProgressChangedEvent(int progress, string progressMessage)
        {
            if (this.ProgressChanged != null)
            {
                this.ProgressChanged(this, new ProgressChangedEventArgs(progress, progressMessage));
            }
        }
    }
}